﻿using System.Net.Http.Json;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Charts;
using Compass.Wasm.Shared.Plans;

namespace Compass.Wasm.Client.Services.Plans;
//前端包装时不要再使用ApiResponse了，直接返回List即可
public interface IMainPlanService : IBaseService<MainPlanDto>
{
    Task<ApiResponse<MainPlanDto>> GetFirstOrDefaultByNumberAsync(string number);
    //主计划绑定项目
    Task<HttpResponseMessage> UpdateStatusesAsync(Guid id, MainPlanDto dto);
    
    Task<List<MainPlanDto>> GetAllByProjectIdAsync(Guid projectId);
    //新查询
    Task<List<MainPlanDto>> GetBlazorMainPlansAsync(int year, int month, bool annual, bool fromMonth,bool openIssue, bool withIssue);

    Task<List<ChartIntDataDto>> GetBlazorMainPlanStatusAsync(int year, int month, bool annual);
    Task<List<ChartDoubleDataDto>> GetBlazorDeliveryReliabilityAsync(int year);
    Task<double> GetBlazorDeliveryReliabilityYtdAsync(int year);
    Task<List<ChartDoubleDataDto>> GetBlazorProductionCycleTimeAsync(int year);
    Task<double> GetBlazorProductionCycleTimeYtdAsync(int year);
    Task<List<ChartDoubleDataDto>> GetBlazorFactoryCycleTimeAsync(int year);
    Task<double> GetBlazorFactoryCycleTimeYtdAsync(int year);
    Task<List<ChartDoubleDataDto>> GetBlazorOrderCycleTimeAsync(int year);
    Task<double> GetBlazorOrderCycleTimeYtdAsync(int year);
}

public class MainPlanService : BaseService<MainPlanDto>, IMainPlanService
{
    private readonly HttpClient _http;
    private const string ServiceName = "MainPlan";
    public MainPlanService(HttpClient http) : base(http, ServiceName)
    {
        _http = http;
    }
    
    public Task<ApiResponse<MainPlanDto>> GetFirstOrDefaultByNumberAsync(string number)
    {
        return _http.GetFromJsonAsync<ApiResponse<MainPlanDto>>($"api/{ServiceName}/Number?number={number}")!;
    }

    public Task<HttpResponseMessage> UpdateStatusesAsync(Guid id, MainPlanDto dto)
    {
        return _http.PutAsJsonAsync($"api/{ServiceName}/UpdateStatuses/{id}", dto);
    }
    
    public async Task<List<MainPlanDto>> GetAllByProjectIdAsync(Guid projectId)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<MainPlanDto>>>($"api/{ServiceName}/Project/{projectId}");
        return response.Status ? response.Result : new List<MainPlanDto>();
    }

    public async Task<List<MainPlanDto>> GetBlazorMainPlansAsync(int year, int month, bool annual, bool fromMonth, bool openIssue,bool withIssue)
    {
        
        var response =await _http.GetFromJsonAsync<ApiResponse<List<MainPlanDto>>>($"api/{ServiceName}/Blazor/All?year={year}&month={month}&annual={annual}&fromMonth={fromMonth}&openIssue={openIssue}&withIssue={withIssue}");
        return response.Status ? response.Result : new List<MainPlanDto>();
    }

    //统计MainPlanStatus
    public async Task<List<ChartIntDataDto>> GetBlazorMainPlanStatusAsync(int year, int month, bool annual)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartIntDataDto>>>($"api/{ServiceName}/Blazor/Status?year={year}&month={month}&annual={annual}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enum.GetNames(typeof(MainPlanStatus_e)).Select(s => new ChartIntDataDto { Label = s, Value = 0 }).ToList();
    }
    public async Task<List<ChartDoubleDataDto>> GetBlazorDeliveryReliabilityAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartDoubleDataDto>>>($"api/{ServiceName}/Blazor/DeliveryReliability?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enumerable.Range(1,12).Select(s => new ChartDoubleDataDto { Label = s.ToString(), Value = 0 }).ToList();
    }
    public async Task<double> GetBlazorDeliveryReliabilityYtdAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<double>>($"api/{ServiceName}/Blazor/DeliveryReliabilityYtd?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : 0;
    }
    //ProductionCycleTime

    public async Task<List<ChartDoubleDataDto>> GetBlazorProductionCycleTimeAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartDoubleDataDto>>>($"api/{ServiceName}/Blazor/ProductionCycleTime?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enumerable.Range(1, 12).Select(s => new ChartDoubleDataDto { Label = s.ToString(), Value = 0 }).ToList();
    }
    public async Task<double> GetBlazorProductionCycleTimeYtdAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<double>>($"api/{ServiceName}/Blazor/ProductionCycleTimeYtd?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : 0;
    }

    public async Task<List<ChartDoubleDataDto>> GetBlazorFactoryCycleTimeAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartDoubleDataDto>>>($"api/{ServiceName}/Blazor/FactoryCycleTime?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enumerable.Range(1, 12).Select(s => new ChartDoubleDataDto { Label = s.ToString(), Value = 0 }).ToList();
    }
    public async Task<double> GetBlazorFactoryCycleTimeYtdAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<double>>($"api/{ServiceName}/Blazor/FactoryCycleTimeYtd?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : 0;
    }

    public async Task<List<ChartDoubleDataDto>> GetBlazorOrderCycleTimeAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<List<ChartDoubleDataDto>>>($"api/{ServiceName}/Blazor/OrderCycleTime?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : Enumerable.Range(1, 12).Select(s => new ChartDoubleDataDto { Label = s.ToString(), Value = 0 }).ToList();
    }
    public async Task<double> GetBlazorOrderCycleTimeYtdAsync(int year)
    {
        var response = await _http.GetFromJsonAsync<ApiResponse<double>>($"api/{ServiceName}/Blazor/OrderCycleTimeYtd?year={year}");
        //查询失败则默认全部给0
        return response.Status ? response.Result : 0;
    }
}